﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Sail.Environment.Extensions.Models;
using Sail.Security;
using Sail.Settings;

namespace Sail
{
    /// <summary>
    /// 站点运行上下文类
    /// </summary>
    public abstract class WorkContext
    {
        /// <summary>
        /// 上下文对象
        /// </summary>
        public HttpContextBase HttpContext
        {
            get { return GetState<HttpContextBase>("HttpContext"); }
            set { SetState("HttpContext", value); }
        }

        /// <summary>
        /// 布局对象
        /// </summary>
        public dynamic Layout
        {
            get { return GetState<object>("Layout"); }
            set { SetState("Layout", value); }
        }

        /// <summary>
        /// 当前站点
        /// </summary>
        public ISite CurrentSite
        {
            get { return GetState<ISite>("CurrentSite"); }
            set { SetState("CurrentSite", value); }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public IUser CurrentUser
        {
            get { return GetState<IUser>("CurrentUser"); }
            set { SetState("CurrentUser", value); }
        }

        /// <summary>
        /// 当前站点皮肤
        /// </summary>
        public ExtensionDescriptor CurrentTheme
        {
            get { return GetState<ExtensionDescriptor>("CurrentTheme"); }
            set { SetState("CurrentTheme", value); }
        }

        /// <summary>
        /// 当前语言环境
        /// </summary>
        public string CurrentCulture
        {
            get { return GetState<string>("CurrentCulture"); }
            set { SetState("CurrentCulture", value); }
        }

        /// <summary>
        /// 解析
        /// </summary>
        /// <returns>T</returns>
        public abstract T Resolve<T>();

        /// <summary>
        /// 解析
        /// </summary>
        /// <param name="service">T</param>
        /// <returns>true or false</returns>
        public abstract bool TryResolve<T>(out T service);

        /// <summary>
        /// 获取实例对象
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>某个实例对象</returns>
        public abstract T GetState<T>(string name);

        /// <summary>
        /// 保存实例对象
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="value">某个实例对象</param>
        public abstract void SetState<T>(string name, T value);
    }
}
